package com.woniu.service;

import com.alibaba.fastjson.JSON;
import com.woniu.adapter.FixedCurrentIncomeAdapter;
import com.woniu.adapter.ProductSellAdapter;
import com.woniu.inlet.web.fo.ConfirmCurrentSellOrder;
import com.woniu.outlet.client.FinancePayFeign;
import com.woniu.outlet.client.SendOrderMsgToPayFeign;
import com.woniu.outlet.client.mq.CurrentSellPayMQ;
import com.woniu.outlet.mysql.po.FixedCurrentIncomeMySQL;
import com.woniu.utils.NumberRegexValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;

import java.math.BigDecimal;

/**
 * 活期产品的功能实现
 */
@Slf4j
@Service
@Transactional
public class CurrentService {

    @Autowired
    protected FixedCurrentIncomeAdapter adapter;

    @Autowired
    private FinancePayFeign payFeign;

    @Autowired
    private ProductSellAdapter sellAdapter;

    @Autowired
    private SendOrderMsgToPayFeign msgToPayFeign;

    /**
     * 卖出活期产品
     * @param currentId
     * @param sellCost
     */
    public Object sellCurrentProduct(String userId,Long currentId,String payPassword) {
        if (!NumberRegexValidationUtil.isPositiveInteger(currentId+"")) {
            return "参数非法";
        }
        int i = 1;
        boolean b = false;
        while (i <= 5) {
            try {
                b = payFeign.isPayPassword(Integer.valueOf(userId), payPassword);
                log.debug("feign支付密码成功");
                break;
            } catch (Exception e) {
                log.debug("卖出验证支付密码超时，重试第{}次", i);
                if (i == 5) {
                    return "验证支付密码超时,请稍后再试";
                }
                i++;
            }
        }
        if (!b) {
            return "支付密码错误";
        }
        FixedCurrentIncomeMySQL mySQL = adapter.sellCurrentProduct(currentId);
        if(mySQL==null){
            return "没有数据";
        }
        if(mySQL.getStatus()==1){
            return "不能重复卖出，请等待卖出通知";
        }
        log.debug("生成卖出记录");
        mySQL.setStatus(1);//修改状态为已卖出1
        adapter.updateById(mySQL);
        String sId = sellAdapter.fixedAndCurrentSell(mySQL);
        LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        String curId=mySQL.getId().toString();
        //第1个是codeId,第2个是名字，第3个是userId，第4个是持有天数，
        //第5个是本金+收益，第6个是产品类型
        map.add(curId,mySQL.getCodeId());
        map.add(curId, mySQL.getName());
        map.add(curId,mySQL.getUserId().toString());
        map.add(curId,mySQL.getHoldDay().toString());
        map.add(curId,mySQL.getTotalPrincipal().add(mySQL.getIncome()).toString());
        map.add(curId,mySQL.getProductType());
        map.add("sell",sId);
        msgToPayFeign.sendMessage(CurrentSellPayMQ.EXCHANGE,CurrentSellPayMQ.ROUTING_KEY,JSON.toJSONString(map));
        log.debug("活期产品等待卖出...");
        return "y";
    }


    /**
     * 活期产品的确认卖出
     * @param currentSellOrder
     */
    public void confirmSellOrder(ConfirmCurrentSellOrder currentSellOrder) {
        Long id = sellAdapter.sellCurrent(currentSellOrder);
        log.debug("得到手续费和卖出金额，确认卖出");
        adapter.deleteByPrimaryKey(id);
        log.debug("该活期产品删除");
    }
}
